/**
 * Created by Administrator on 2018-07-30.
 * 进程监控
 */

define(function (require,exports,module) {
    var sTpl = require("./processDetails.html");
    require("./processDetails.css");
    var DEFINE_STATUS = require("../util/status.js");
    var socket=require("../util/socketServer.js");
    var VueComponent = Vue.extend({
        template: sTpl,
        components:{
            'messages':socket
        },
        data:function () {
            return {
                DEFINE_STATUS:DEFINE_STATUS,
                currentFlight:'',
                nodeData:[],
                baseNodes:[],
                messageComponent:{messageType:"PROCESS_CONTROL"},
                flightPlanes:[],
                nodes:[{
                    nodeName:'预计落地',
                    preArrive:[],
                },
                    {
                        nodeName:'近空',
                        preArrive:[]
                    },
                    {
                        nodeName:'本站着陆',
                        preArrive:[]
                    },
                    {
                        nodeName:'上轮档',
                        preArrive:[]
                    },
                    {
                        nodeName:'保障',
                        preArrive:[]
                    },
                    {
                        nodeName:'本站待飞',
                        preArrive:[]
                    },{
                        nodeName:'撤轮档',
                        preArrive:[]
                    },
                    {
                        nodeName:'本站起飞',
                        preArrive:[]
                    }
                ],
                flightNums: [{
                    value: 'CD1857',
                    label: 'CD1857'
                }, {
                    value: 'BJ5867',
                    label: 'BJ5867'
                }, {
                    value: 'GZ3419',
                    label: 'GZ3419'
                }, {
                    value: 'SH2475',
                    label: 'SH2475'
                }],
                checkFormInline:{
                    flightNum:''
                }

            }
        },
        methods:{
            findAllCnb:function() {
                var $this = this;
                axios.get(basepath+"/api/sysSetting/findCnb"
                ).then(function (response) {
                    $this.nodeData=[];
                    var b={};
                    response.data.sort(function (va1,va2) {
                        return va1.nodeSeq > va2.nodeSeq?1:-1;
                    });
                    response.data.forEach(function(value){

                        var nodeSeq =value.nodeSeq+"a";
                        if(!b[nodeSeq]){
                            b[nodeSeq]={};
                        }
                        if(value.locatingPlace=='top'){
                            b[nodeSeq].top=value
                        }
                        if(value.locatingPlace=='bottom'){
                            b[nodeSeq].bottom=value
                        }
                    })
                    for(var i in b){
                        $this.baseNodes.push(b[i]);
                        $this.nodeData=JSON.parse(JSON.stringify($this.baseNodes))
                    }
                });
            },
            nodeRefresh:function () {
                var nodeData=JSON.parse(JSON.stringify(this.baseNodes));
                var nodeMap={};
                nodeData.forEach(function (node ,i) {
                    if(node.top){
                        nodeMap[node.top.nodeName] = node.top;
                    }if(node.bottom){
                        nodeMap[node.bottom.nodeName] = node.bottom;
                    }

                })
                if(this.currentFlight&&this.currentFlight.courseNodePos)
                this.currentFlight.courseNodePos.forEach(function (t) {
                    if(nodeMap[t.nodeName]){
                        nodeMap[t.nodeName].nodeTime =  t.nodeTime;
                    }

                })
                this.nodeData= nodeData;
            },
            checkFlight:function (item) {
                this.currentFlight=item;
                this.checkFormInline.flightNum='';
                this.nodeRefresh();
            },
            check:function () {
                this.currentFlight=this.checkFormInline.flightNum
            },
            findTodayPlane:function () {
                var $this= this;
               axios.get(basepath+"/api/flightPlane/findTodayPlane").then(function (response) {
                   $this.flightPlanes=response.data;
                   $this.flightPlaneStatusRefresh();

               })
            },
            flightPlaneStatusRefresh:function () {
                try{
                    var processNodes={};
                    this.nodes.forEach(function (node) {
                        processNodes[node.nodeName]=node;
                        node.preArrive=[];
                    });
                    this.flightPlanes.forEach(function (event) {

                        if(event.importOrExport=="进港"){
                            try{
                                if(event.arriveTime){
                                    var time = new Date(event.arriveTime).getTime();
                                    var diff = time - new Date().getTime();
                                    if(diff>0&&diff<30*60*1000 &&event.processStatus<DEFINE_STATUS.statusNum.EXPECTED_TO_FALL){
                                        processNodes["预计落地"].preArrive.push(event);
                                    }
                                }
                            }catch (e){

                            }

                        }
                        if(event.processStatus==this.DEFINE_STATUS.statusNum.EXPECTED_TO_FALL){
                            processNodes["预计落地"].preArrive.push(event)
                        }
                        if(event.processStatus==this.DEFINE_STATUS.statusNum.NEAR_SPACE){
                            processNodes["近空"].preArrive.push(event)
                        }
                        if(event.processStatus==this.DEFINE_STATUS.statusNum.LANDED){
                            processNodes["本站着陆"].preArrive.push(event)
                        }
                        if(event.processStatus==this.DEFINE_STATUS.statusNum.UPPER_WHEEL){
                            processNodes["上轮档"].preArrive.push(event)
                        }
                        if(event.processStatus==this.DEFINE_STATUS.statusNum.ENSURE_START){
                            processNodes["保障"].preArrive.push(event)
                        }
                        if(event.processStatus==this.DEFINE_STATUS.statusNum.EXPECTED_TO_FLY){
                            processNodes["本站待飞"].preArrive.push(event)
                        }

                        if(event.processStatus==this.DEFINE_STATUS.statusNum.PULLEY_GEAR){
                            processNodes["撤轮档"].preArrive.push(event)
                        }
                        if(event.processStatus==this.DEFINE_STATUS.statusNum.TAKE_OFF){
                            processNodes["本站起飞"].preArrive.push(event)
                        }
                    })
                }catch (e){
                    console.error(e);
                }

            }

        },
        watch:{
        },
        computed:{
            time1:function () {
                var date=new Date();
                return date.getHours()+":"+date.getMinutes();
            }
        },
        deactivated:function () {

        },
        activated:function () {
            this.findTodayPlane();
            messages.$emit('change','进程监控');

        },
        created:function () {
            this.findAllCnb();
            this.findTodayPlane();
            var $this= this;

            messages.$on(this.messageComponent.messageType,function (event) {
                var messageFilter = $this.$refs.messageComponent.messageFilter.PROCESS_CONTROL;
                var a=JSON.parse(event.data);
                var eventType = a.eventType;
                a= a.messageBody;
                if(messageFilter&&messageFilter.COURSE_NODE_COMPLETED==eventType){
                    for(var i=0;i<$this.flightPlanes.length;i++){
                        if(a.flightPlanePo.planeId==$this.flightPlanes[i].planeId){
                            $this.flightPlanes[i].courseNodePos.push(a);
                            $this.nodeRefresh();
                        }
                    }
                }
                if(messageFilter&&messageFilter.PROCESS_STATUS_CHANGE==eventType){
                    var hasLoad = false;
                    for(var i=0;i<$this.flightPlanes.length;i++){
                        if(a.planeId==$this.flightPlanes[i].planeId){
                            $this.$set($this.flightPlanes,'i',a);
                        }
                    }
                    if(!hasLoad){
                        $this.flightPlanes.push(a);
                    }
                    $this.flightPlaneStatusRefresh();
                }


            });
            setInterval(this.flightPlaneStatusRefresh,10000);
        }
    })
    module.exports=VueComponent;
    module.exports.reDefine=function (val) {
        module.exports.type=val;
        return module.exports;
    }

})


